package com.clock.shop.service.impl;


import com.clock.shop.domain.ShopIntegral;
import com.clock.shop.domain.dto.ExchangeCouponDTO;
import com.clock.shop.mapper.ShopIntegralMapper;
import com.clock.shop.service.IShopIntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 积分Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-10
 */
@Service
public class ShopIntegralServiceImpl implements IShopIntegralService
{
    @Resource
    private ShopIntegralMapper shopIntegralMapper;

    /**
     * 查询积分
     * 
     * @param integralId 积分主键
     * @return 积分
     */
    @Override
    public ShopIntegral selectShopIntegralByIntegralId(Long integralId)
    {
        return shopIntegralMapper.selectShopIntegralByIntegralId(integralId);
    }

    /**
     * 查询积分列表
     * 
     * @param shopIntegral 积分
     * @return 积分
     */
    @Override
    public List<ShopIntegral> selectShopIntegralList(ShopIntegral shopIntegral)
    {
        return shopIntegralMapper.selectShopIntegralList(shopIntegral);
    }

    /**
     * 新增积分
     * 
     * @param shopIntegral 积分
     * @return 结果
     */
    @Override
    public int insertShopIntegral(ShopIntegral shopIntegral)
    {
        return shopIntegralMapper.insertShopIntegral(shopIntegral);
    }

    /**
     * 修改积分
     * 
     * @param shopIntegral 积分
     * @return 结果
     */
    @Override
    public int updateShopIntegral(ShopIntegral shopIntegral)
    {
        return shopIntegralMapper.updateShopIntegral(shopIntegral);
    }

    /**
     * 批量删除积分
     * 
     * @param integralIds 需要删除的积分主键
     * @return 结果
     */
    @Override
    public int deleteShopIntegralByIntegralIds(Long[] integralIds)
    {
        return shopIntegralMapper.deleteShopIntegralByIntegralIds(integralIds);
    }

    /**
     * 删除积分信息
     * 
     * @param integralId 积分主键
     * @return 结果
     */
    @Override
    public int deleteShopIntegralByIntegralId(Long integralId)
    {
        return shopIntegralMapper.deleteShopIntegralByIntegralId(integralId);
    }

    /**
     * 用户使用积分兑换优惠券
     * @param exchangeCouponDTO 用户使用积分兑换优惠券参数实体
     * @return 操作情况
     */
    @Override
    public int exchangeCoupon(ExchangeCouponDTO exchangeCouponDTO) {
        // 获取用户ID
        Long userId = exchangeCouponDTO.getUserId();
        ShopIntegral shopIntegral1 = new ShopIntegral();
        // 查询用户在该店铺积分余额
        shopIntegral1.setUserId(userId);
        shopIntegral1.setShopId(exchangeCouponDTO.getShopId());
        List<ShopIntegral> shopIntegrals = shopIntegralMapper.selectShopIntegralList(shopIntegral1);
        // 判断用户积分余额是否足够
        if(exchangeCouponDTO.getCouponNeedIntegral()>shopIntegrals.get(0).getIntegralNumber()){
            return -1;
        }else{
            for (Long couponId:exchangeCouponDTO.getCouponIds()
            ) {
                ShopIntegral shopIntegral = new ShopIntegral();
                // 更新用户优惠券包
                shopIntegralMapper.exchangeCoupon(userId,couponId);
                // 扣减用户在该店铺积分余额
                shopIntegral.setUserId(userId);
                shopIntegral.setShopId(exchangeCouponDTO.getShopId());
                shopIntegral.setIntegralNumber(exchangeCouponDTO.getIntegralNum()-exchangeCouponDTO.getCouponNeedIntegral());
                shopIntegralMapper.updateShopIntegral(shopIntegral);
            }
        }

        return 0;
    }
}
